<?php

session_start();
require_once '../MyDB.php';
$con = new MyDB();

$type = $_POST["type"];

if ($type == 1) {
    // Inserta
    $nombre = $_POST["nombre"];
    $lugares = $_POST["lugares"];
    $precio = $_POST["precio"];
    $mensual = $_POST["mensual"];
    $extra = $_POST["extra"];
    $tipo = $_POST["tipo"];

    $precio = ($precio == "" || $precio == null) ? 0 : $precio;
    $mensual = ($mensual == "" || $mensual == null) ? 0 : $mensual;
    $extra = ($extra == "" || $extra == null) ? 0 : $extra;

    $query = $con->insertInto("rooms", $fields = array("Nombre" => $nombre, "Lugares" => $lugares, "Precio" => $precio, "Mensual" => $mensual,
        "Extra" => $extra, "IdRoomType" => $tipo));

    //print_r($query["sql"]);

    $result = $query["status"];
    if ($result == "success") {
        echo 'Dato guardado con éxito';
    } else {
        echo "El dato no ha sido guardado";
    }
} elseif ($type == 0) {
    // Búsqueda

    $_orderBy = $_POST["orderby"];

    $query = $con->selectFrom("rooms", $columns = null, $where = null, $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);

    $result = $query["result"];

    if ($query["num"] > 0) {
         foreach ($result as $row) {


            $param = "'" . $row["IdRoom"] . "','" . $row["Nombre"] . "','" . $row["Lugares"] . "','" . $row["Precio"] . "','"
                    . $row["Mensual"] . "','" . $row["Extra"] . "','" . $row["IdRoomType"] . "'";
            
            echo "<tr><td>" . $row["IdRoom"] . "</td>"
            . "<td>" . $row["Nombre"] . "</td><td>" . $row["Lugares"] . "</td>"
            . "<td>" . $row["Precio"] . "</td><td>" . $row["Mensual"] . "</td>"
            . "<td>" . $row["Extra"] . "</td><td>" . $row["IdRoomType"] . "</td>"
            . "<td onClick='deleteRoom(" . $row["IdRoom"] . ")' "
            . "style='cursor:pointer;'><img style='height:20px; width:20px;' src='./img/delete.png'></td>"
            . '<td onClick="selectRoom(' . $param . ')" '
            . "style='cursor:pointer;'><img style='height:20px; width:20px;' src='./img/edit.png'></td></tr>";
        }
    }
} elseif ($type == 2) {
    // Borra
    $id = $_POST["id"];
    $query = $con->deleteFrom("rooms", $where = array("IdRoom" => $id), $like = false, $limit = 1);
    $result = $query["status"];
    
    //print_r($query['sql']);
    if ($result == "success") {
        echo 'Dato borrado con éxito';
    } else {
        echo "El dato no ha sido borrado";
    }
} elseif ($type == 3) {
    // Modificar
    $id = $_POST["id"];
    $nombre = $_POST["nombre"];
    $lugares = $_POST["lugares"];
    $precio = $_POST["precio"];
    $mensual = $_POST["mensual"];
    $extra = $_POST["extra"];
    $tipo = $_POST["tipo"];

    $precio = ($precio == "" || $precio == null) ? 0 : $precio;
    $mensual = ($mensual == "" || $mensual == null) ? 0 : $mensual;
    $extra = ($extra == "" || $extra == null) ? 0 : $extra;

    $query = $con->updateTable("rooms", $fields = array("Nombre" => $nombre, "Lugares" => $lugares, "Precio" => $precio, "Mensual" => $mensual,
        "Extra" => $extra, "IdRoomType" => $tipo), $where = array("IdRoom" => $id));
    $result = $query["status"];
    if ($result == "success") {
        echo 'Dato guardado con éxito';
    } else {
        echo "El dato no ha sido guardado";
    }
}elseif($type == 4){
    // Trae cantidad de camas
    $id = $_POST["id"];
    $query = $con->selectFrom("rooms", $columns = null, $where = array("IdRoom" => $id), $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);

    $result = $query["result"];
    if ($query["num"] > 0) {
         foreach ($result as $row) {

            
            echo $row["Lugares"];
        }
    }
}elseif ($type == 5) {
    // Búsqueda por id

    $id = $_POST["id"];

    $query = $con->selectFrom("rooms", $columns = null, $where = array("IdRoom" => $id), $like = false, $orderby = null, $direction = "DESC", $limit = null, $offset = null);
    
    $result = $query["result"];
    
    if ($query["num"] > 0) {
         foreach ($result as $row) {


            echo "" . $row["IdRoom"] . " " . $row["Nombre"] . " " . $row["Lugares"] . " " . $row["Precio"] . " "
                    . $row["Mensual"] . " " . $row["Extra"] . " " . $row["IdRoomType"] . "";
            
        }
    }
}



?>